<template>
	<view>
		<view class="box">
			<view class="title"><text>*</text>商户名称</view>
			<view class="detail">
				<input type="text" placeholder-class="mo" placeholder="请输入" v-model="shopname" />
			</view>
			<view class="title"><text>*</text>手机号码</view>
			<view class="detail">
				<input type="number" maxlength="11" placeholder-class="mo" placeholder="请输入" v-model="mobile" />
			</view>
			<view class="title"><text>*</text>店铺分类</view>
			<picker @change="bindPickerChange" :value="index" range-key="name" :range="array">
				<view class="detail">
					<view v-if="index>-1">{{array[index].name}}</view>
					<view v-else class="mo">请选择</view>
				</view>
			</picker>

			<view class="title"><text>*</text>店铺城市</view>
			<picker mode="region" :range="city" @change="selectCity">
				<view class="detail">
					<view v-if="city[0]">{{city[0]}} {{city[1]}} {{city[2]}}</view>
					<view v-else class="mo">请选择</view>
				</view>
			</picker>



			<view class="title"><text>*</text>店铺地址</view>
			<view class="detail" @click="selectAddress">
				<view v-if="address">{{address}}</view>
				<view v-else class="mo">请选择</view>
			</view>
			<view class="title"><text>*</text>联系姓名</view>
			<view class="detail">
				<input type="text" placeholder-class="mo" placeholder="请输入" v-model="name" />
			</view>
			<view class="title"><text>*</text>入驻类型</view>
			<view class="detail-new" style="border: 0;">
				<view class="lc-de" @click="tab(1)">
					<image :src="state==1?baseurl+'/imgs/ss5.png':baseurl+'/imgs/ss6.png'" mode="widthFix"></image>
					<view>餐饮商家</view>
				</view>
				<view class="lc-de" @click="tab(2)">
					<image :src="state==2?baseurl+'/imgs/ss5.png':baseurl+'/imgs/ss6.png'" mode="widthFix"></image>
					<view>餐饮安全承诺商家<text>(需支付1000元保证金)</text></view>
				</view>
			</view>
		</view>
		<view class="box">
			<view class="title"><text>*</text>营业执照照片</view>
			<view class="detail-img dd">
				<image :src="image?baseurl+image:baseurl+'/imgs/ss4.png'" mode="widthFix" @click="upload(1)"></image>
			</view>
			<view class="title"><text>*</text>统一社会信用代码</view>
			<view class="detail" style="border: 0;">
				<input type="text" placeholder-class="mo" placeholder="请输入" v-model="number" />
			</view>
		</view>
		<view class="box">
			<view class="title"><text>*</text>法人身份证正面</view>
			<view class="detail-img">
				<image :src="contact_id_doc_copy?baseurl+contact_id_doc_copy:baseurl+'/imgs/ss4.png'" mode="widthFix"
					@click="upload(2)"></image>
			</view>
		</view>
		<view class="box">
			<view class="title"><text>*</text>法人身份证背面</view>
			<view class="detail-img">
				<image :src="contact_id_doc_copy_back?baseurl+contact_id_doc_copy_back:baseurl+'/imgs/ss4.png'"
					mode="widthFix" @click="upload(3)"></image>
			</view>
		</view>
		<view class="box">
			<view class="title"><text>*</text>食品经营许可证</view>
			<view class="detail-img">
				<image :src="qualification_image?baseurl+qualification_image:baseurl+'/imgs/ss4.png'" mode="widthFix"
					@click="upload(4)"></image>
			</view>
		</view>
		<view class="box">
			<view class="title"><text>*</text>负责人健康证</view>
			<view class="detail-img">
				<image :src="health?baseurl+health:baseurl+'/imgs/ss4.png'" mode="widthFix" @click="upload(5)"></image>
			</view>
		</view>
		<view class="box">
			<view class="title"><text>*</text>店铺LOGO</view>
			<view class="detail-img">
				<image :src="logo?baseurl+logo:baseurl+'/imgs/ss4.png'" mode="widthFix" @click="upload(6)"></image>
			</view>
		</view>
		<view class="box">
			<view class="title"><text>*</text>店内照片</view>
			<view class="detail-img">
				<image :src="baseurl+'/imgs/ss4.png'" mode="widthFix" @click="uploads"></image>
				<view class="add-img" v-for="(img,ci) in tmpimg" :key="ci">
					<view class="delete" @click="close(ci)">x</view>
					<image :src="baseurl+img" mode="aspectFill"></image>
				</view>
			</view>
		</view>
		<view class="lc-big"></view>
		<view class="sub" @click="sub">提交</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				app: getApp().globalData,
				baseurl: getApp().globalData.baseurl,
				state: 1,
				shopname: '',
				mobile: '',
				address: '',
				name: '',
				image: '',
				number: '',
				contact_id_doc_copy: '',
				contact_id_doc_copy_back: '',
				state: '',
				qualification_image: '',
				health: '',
				logo: '',
				lat: '',
				lng: '',
				city: ['', '', ''],
				tmpimg: [],
				array: [],
				index: -1
			}
		},
		onLoad(e) {
			// this.state = e.state
			this.app.post('/api/chef/index/sel_shop_cat', {
				type: 2
			}).then(res => {
				console.log(res)
				this.array = res
			})
		},
		methods: {
			bindPickerChange(e) {
				this.index = e.detail.value
			},
			selectCity(e) {
				this.city = e.detail.value
			},
			selectAddress() {
				uni.chooseLocation({
					success: (res) => {
						console.log(res)
						this.lat = res.latitude
						this.lng = res.longitude
						this.address = res.address
					}
				});
			},
			sub() {
				if (this.index == -1) {
					uni.showToast({
						title: '请选择店铺分类',
						icon: "none"
					})
					return
				}
				this.app.post('/api/increased/index/ApplyShop', {
					shopname: this.shopname,
					mobile: this.mobile,
					address: this.address,
					name: this.name,
					image: this.image,
					number: this.number,
					contact_id_doc_copy: this.contact_id_doc_copy,
					contact_id_doc_copy_back: this.contact_id_doc_copy_back,
					state: this.state,
					qualification_image: this.qualification_image,
					health: this.health,
					logo: this.logo,
					lat: this.lat,
					lng: this.lng,
					contact_type: 'LEGAL',
					city: this.city[0] + '/' + this.city[1] + '/' + this.city[2],
					legal_person: '',
					contact_email: '',
					id_card_address: '',
					id_card: '',
					contact_period_begin: '',
					contact_period_end: '',
					is_id_card_long_term: '',
					service_phone: '',
					account_bank: '',
					bank_address_code: '',
					bank_name: '',
					account_number: '',
					status: 'norma',
					shop_image: this.tmpimg.join(','),
					cat_id: this.array[this.index].id
				}).then(res => {
					console.log(res)
					if (this.state == 2) {
						uni.navigateTo({
							url: '/pages/lc/payPages/payPages?state=2&order_sn=' + res.order_sn
						})
					} else {
						uni.showToast({
							title: '申请已提交'
						})
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/lcMy/lcMy'
							})
						}, 1500)
					}

				})
			},
			tab(state) {
				this.state = state
			},
			upload(num) {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					success: (res) => {
						let filePath = res.tempFilePaths[0]
						uni.showLoading({
							title: '上传中'
						})
						uni.uploadFile({
							url: this.baseurl + '/api/common/upload',
							header: {
								'token': uni.getStorageSync('wanlshop:user').token,
							},
							filePath,
							name: 'file',
							success: (res) => {
								uni.hideLoading()
								console.log(JSON.parse(res.data))
								if (num == 1) {
									this.image = JSON.parse(res.data).data.url
								}
								if (num == 2) {
									this.contact_id_doc_copy = JSON.parse(res.data).data.url
								}
								if (num == 3) {
									this.contact_id_doc_copy_back = JSON.parse(res.data).data.url
								}
								if (num == 4) {
									this.qualification_image = JSON.parse(res.data).data.url
								}
								if (num == 5) {
									this.health = JSON.parse(res.data).data.url
								}
								if (num == 6) {
									this.logo = JSON.parse(res.data).data.url
								}
							}
						})
					}
				})
			},
			uploads() {
				let url = getApp().globalData.baseurl;
				let that = this
				uni.chooseImage({
					count: 9,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: function(res) {
						console.log(JSON.stringify(res.tempFilePaths));
						res.tempFilePaths.forEach((e, i) => {
							uni.showLoading({
								title: `正在上传\n ${i+1}/${res.tempFilePaths.length}张`
							})
							uni.uploadFile({
								url: url + '/api/common/upload',
								filePath: e,
								name: 'file',
								success: (res) => {
									uni.hideLoading()
									console.log(JSON.parse(res.data))
									that.tmpimg.push(JSON.parse(res.data).data.url)
								}
							})
						})
					}
				})
			},
			// 删除图片
			close(e) {
				this.tmpimg.splice(e, 1)
			}
		}
	}
</script>

<style lang="scss">
	.box {
		width: 690rpx;
		background-color: #FFF;
		margin-left: 30rpx;
		margin-top: 30rpx;
		border-radius: 20rpx;
		display: flex;
		flex-wrap: wrap;

		.title {
			margin-left: 30rpx;
			font-size: 30rpx;
			color: #333333;
			margin-top: 30rpx;

			text {
				color: #FF0000;
			}
		}

		.detail-new {
			width: 630rpx;
			display: flex;
			height: 100rpx;
			align-items: center;
			margin-left: 30rpx;
			justify-content: space-between;

			.lc-de {
				display: flex;
				align-items: center;

				image {
					width: 28rpx;
					height: 28rpx;
				}

				view {
					font-size: 26rpx;
					color: #999999;
					margin-left: 8rpx;

					text {
						font-size: 22rpx;
						color: #84BD00;
					}
				}
			}
		}

		.dd {
			border-bottom: 1px solid #eee;
		}

		.detail-img {
			width: 630rpx;
			margin-left: 30rpx;
			display: flex;

			padding-bottom: 30rpx;
			flex-wrap: wrap;

			image {
				width: 172rpx;
				height: 172rpx;
				border-radius: 10rpx;
				margin-right: 26rpx;
				margin-top: 30rpx;
			}

			.add-img {
				width: 172rpx;
				height: 172rpx;
				margin-top: 30rpx;
				border-radius: 10rpx;
				margin-right: 26rpx;
				position: relative;

				.delete {
					display: flex;
					justify-content: center;
					align-items: center;
					position: absolute;
					right: 0rpx;
					top: 0rpx;
					z-index: 999;
					font-size: 45rpx;
				}
			}
		}

		.detail {
			border-bottom: 1px solid #eee;
			width: 630rpx;
			margin-left: 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 100rpx;

			view {
				font-size: 30rpx;
			}

			.mo {
				color: #999999;
			}

			.all-next {
				font-size: 26rpx;
			}

			image {
				width: 20rpx;
				height: 20rpx;
			}

			input {
				width: 100%;
				font-size: 30rpx;
			}
		}
	}

	.sub {
		width: 690rpx;
		height: 82rpx;
		background-color: #84BD00;
		border-radius: 50rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #FFF;
		font-size: 30rpx;
		position: fixed;
		bottom: 50rpx;
		left: 30rpx;
		z-index: 666;
	}
</style>